package org.example.httpserver.socketio;

import com.alibaba.fastjson.JSONObject;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.annotation.OnConnect;
import com.corundumstudio.socketio.annotation.OnDisconnect;
import com.corundumstudio.socketio.annotation.OnEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Slf4j
//@Component
public class SocketService {


    @OnConnect
    public void onConnect(SocketIOClient client) {
        log.info("客户端：{} sessionId:{}", client.getRemoteAddress().toString(), client.getSessionId());
    }

    @OnDisconnect
    public void onDisconnect(SocketIOClient client) {
        log.info("客户端：{}断开连接", client.getSessionId());
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("type", "disconnect");
        paramMap.put("sessionId", client.getSessionId().toString());
        log.error(paramMap.toString());
    }

    @OnEvent("send_text")
    public void onSendText(SocketIOClient client, String message) {
        log.info("Message received: {}", message);
        client.sendEvent("receive_text", "Server received message:" + message);
    }

    @OnEvent("send_image")
    public void onSendImage(SocketIOClient client, JSONObject json) {
        log.info("Message received: {}", json);
        client.sendEvent("receive_image", json);
    }
}
